/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controladoresJPA;

import controladoresJPA.exceptions.NonexistentEntityException;
import controladoresJPA.exceptions.RollbackFailureException;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import javax.transaction.UserTransaction;
import modelo.Consumible;
import modelo.OperarConsumo;
import modelo.OperarConsumoConsumible;

/**
 *
 * @author Francisco
 */
public class OperarConsumoConsumibleJpaController implements Serializable {

    public OperarConsumoConsumibleJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(OperarConsumoConsumible operarConsumoConsumible) throws RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Consumible idConsumible = operarConsumoConsumible.getIdConsumible();
            if (idConsumible != null) {
                idConsumible = em.getReference(idConsumible.getClass(), idConsumible.getIdConsumible());
                operarConsumoConsumible.setIdConsumible(idConsumible);
            }
            OperarConsumo idOperarConsumo = operarConsumoConsumible.getIdOperarConsumo();
            if (idOperarConsumo != null) {
                idOperarConsumo = em.getReference(idOperarConsumo.getClass(), idOperarConsumo.getIdOperarConsumo());
                operarConsumoConsumible.setIdOperarConsumo(idOperarConsumo);
            }
            em.persist(operarConsumoConsumible);
            if (idConsumible != null) {
                idConsumible.getOperarConsumoConsumibleList().add(operarConsumoConsumible);
                idConsumible = em.merge(idConsumible);
            }
            if (idOperarConsumo != null) {
                idOperarConsumo.getOperarConsumoConsumibleList().add(operarConsumoConsumible);
                idOperarConsumo = em.merge(idOperarConsumo);
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(OperarConsumoConsumible operarConsumoConsumible) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            OperarConsumoConsumible persistentOperarConsumoConsumible = em.find(OperarConsumoConsumible.class, operarConsumoConsumible.getIdOperarConsumoConsumible());
            Consumible idConsumibleOld = persistentOperarConsumoConsumible.getIdConsumible();
            Consumible idConsumibleNew = operarConsumoConsumible.getIdConsumible();
            OperarConsumo idOperarConsumoOld = persistentOperarConsumoConsumible.getIdOperarConsumo();
            OperarConsumo idOperarConsumoNew = operarConsumoConsumible.getIdOperarConsumo();
            if (idConsumibleNew != null) {
                idConsumibleNew = em.getReference(idConsumibleNew.getClass(), idConsumibleNew.getIdConsumible());
                operarConsumoConsumible.setIdConsumible(idConsumibleNew);
            }
            if (idOperarConsumoNew != null) {
                idOperarConsumoNew = em.getReference(idOperarConsumoNew.getClass(), idOperarConsumoNew.getIdOperarConsumo());
                operarConsumoConsumible.setIdOperarConsumo(idOperarConsumoNew);
            }
            operarConsumoConsumible = em.merge(operarConsumoConsumible);
            if (idConsumibleOld != null && !idConsumibleOld.equals(idConsumibleNew)) {
                idConsumibleOld.getOperarConsumoConsumibleList().remove(operarConsumoConsumible);
                idConsumibleOld = em.merge(idConsumibleOld);
            }
            if (idConsumibleNew != null && !idConsumibleNew.equals(idConsumibleOld)) {
                idConsumibleNew.getOperarConsumoConsumibleList().add(operarConsumoConsumible);
                idConsumibleNew = em.merge(idConsumibleNew);
            }
            if (idOperarConsumoOld != null && !idOperarConsumoOld.equals(idOperarConsumoNew)) {
                idOperarConsumoOld.getOperarConsumoConsumibleList().remove(operarConsumoConsumible);
                idOperarConsumoOld = em.merge(idOperarConsumoOld);
            }
            if (idOperarConsumoNew != null && !idOperarConsumoNew.equals(idOperarConsumoOld)) {
                idOperarConsumoNew.getOperarConsumoConsumibleList().add(operarConsumoConsumible);
                idOperarConsumoNew = em.merge(idOperarConsumoNew);
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = operarConsumoConsumible.getIdOperarConsumoConsumible();
                if (findOperarConsumoConsumible(id) == null) {
                    throw new NonexistentEntityException("The operarConsumoConsumible with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            OperarConsumoConsumible operarConsumoConsumible;
            try {
                operarConsumoConsumible = em.getReference(OperarConsumoConsumible.class, id);
                operarConsumoConsumible.getIdOperarConsumoConsumible();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The operarConsumoConsumible with id " + id + " no longer exists.", enfe);
            }
            Consumible idConsumible = operarConsumoConsumible.getIdConsumible();
            if (idConsumible != null) {
                idConsumible.getOperarConsumoConsumibleList().remove(operarConsumoConsumible);
                idConsumible = em.merge(idConsumible);
            }
            OperarConsumo idOperarConsumo = operarConsumoConsumible.getIdOperarConsumo();
            if (idOperarConsumo != null) {
                idOperarConsumo.getOperarConsumoConsumibleList().remove(operarConsumoConsumible);
                idOperarConsumo = em.merge(idOperarConsumo);
            }
            em.remove(operarConsumoConsumible);
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<OperarConsumoConsumible> findOperarConsumoConsumibleEntities() {
        return findOperarConsumoConsumibleEntities(true, -1, -1);
    }

    public List<OperarConsumoConsumible> findOperarConsumoConsumibleEntities(int maxResults, int firstResult) {
        return findOperarConsumoConsumibleEntities(false, maxResults, firstResult);
    }

    private List<OperarConsumoConsumible> findOperarConsumoConsumibleEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(OperarConsumoConsumible.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public OperarConsumoConsumible findOperarConsumoConsumible(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(OperarConsumoConsumible.class, id);
        } finally {
            em.close();
        }
    }

    public int getOperarConsumoConsumibleCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<OperarConsumoConsumible> rt = cq.from(OperarConsumoConsumible.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
